from pyecharts import options as opts
from pyecharts.charts import Timeline, Bar
import random


def dynamic_visualization(news_list):
    # 模拟时间变化的数据（实际应用中需按时间爬取）
    time_data = {}
    for i in range(5):
        sample = random.sample(news_list, min(5, len(news_list)))
        time_data[f"2025-06-0{i + 1}"] = {n['title']: random.randint(10, 100) for n in sample}

    # 创建时间轴
    timeline = Timeline()
    for date, data in time_data.items():
        bar = (
            Bar()
            .add_xaxis(list(data.keys()))
            .add_yaxis("热度", list(data.values()))
            .set_global_opts(
                title_opts=opts.TitleOpts(f"{date}话题热度"),
                xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45))
            )
        )
        timeline.add(bar, date)

    timeline.render("timeline.html")